const $ = layui.jquery;
const table=layui.table;
const laydate=layui.laydate;
const form=layui.form;
const layer=layui.layer;
const upload=layui.upload;
$(()=> {
    if(error){
        layer.msg(error);
        return;
    }
//     渲染账号过期，权限过期，上次登录的日期时间
    laydate.render({
        elem:"[name=userExpireTime]",
        type:"datetime"
    })
    laydate.render({
        elem:"[name=credExpireTime]",
        type:"datetime"
    })
    laydate.render({
        elem:"[name=lastLoginTime]",
        type:"datetime"
    })
    //     渲染图片上传
    upload.render({
        elem: '#avatar-upload-btn',
        url: ctx+'/sys/api/v1/upload/image',
        data:{//传的数据
            type:"admin_avatar",
        },
        exts:'jpg|png|gif|bmp|jpeg|svg', //上传的类型
        size:1024,
        done: function(resp){//上传完成，响应200状态码进入
            let url =ctx+"/assets/"+resp.data;
            $(".avatar-preview").css("background-image","url("+url+")");
            sessionStorage.setItem("admin_avatar",resp.data);//将图片地址存到会话存储域
        },
        error: function(){
            layer.msg("上传图片失败");
        },

    });
    // 回显数据,admin是list.js里点击edit后后端map放在域里的获,要在edit.html里取出才能用
    admin.password="";

    form.val("edit-form",admin);
    // 日期时间格式转换
    if(admin.credExpireTime){
        form.val('edit-form', {
            credExpireTime: layui.util.toDateString(new Date(admin.credExpireTime),'yyyy-MM-dd HH:mm:ss')
        })
    }
    if(admin.userExpireTime){
        form.val('edit-form', {
            userExpireTime: layui.util.toDateString(new Date(admin.userExpireTime),'yyyy-MM-dd HH:mm:ss')
        })
    }
    // boolean按钮回显
    let lockedValue = admin.locked ? "是" : "否";
    form.val('edit-form', {
        locked: lockedValue
    });
    // 图片回显
    if(admin.avatarUrl){
        let url = ctx + "/assets/"+admin.avatarUrl;
        $(".avatar-preview").css("background-image","url("+url+")");
        sessionStorage.setItem("admin_avatar",admin.avatarUrl);
    }


})
// 点击提交按钮后的异步提交操作
function doSubmit(cb) {
    if(error){
        layer.msg(error);
        return;
    }
    let pass = form.validate("#edit-form")//参数验证
    if (!pass) {
        return;
    }
    // 验证通过，向后台发请求
    const url = ctx + "/sys/api/v1/admin";
    let formData = form.val("edit-form");//参数为过滤器
    formData.id = admin.id;
    // 图片填充
    let avatarUrl=sessionStorage.getItem("admin_avatar");
    if(avatarUrl){
        formData.avatarUrl=avatarUrl;
    }
    // 将locked字符串转换成布尔值
    if(formData.locked === "是"){
        formData.locked = true;
    }else if(formData.locked === "否"){
        formData.locked = false;
    }else{
        formData.locked = null;
    }
    $.ajax({
        url,
        method: "put",
        data: formData,
        success(resp) {
            if (resp.success) {
                if (typeof cb === "function") {
                    cb(resp.success)
                }
            }
        }, error(resp) {
            let json = resp.responseJSON;
            let msg = json.msg || "修改会员异常";
            layer.msg(msg);
        }
    })

}